<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input v-model="listQuery.name" placeholder="标题" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter" />
      <el-select v-model="listQuery.status" placeholder="状态" clearable class="filter-item" style="width: 130px">
        <el-option v-for="item in statusOptions" :key="item.value" :label="item.label" :value="item.value" />
      </el-select>
      <el-select v-model="listQuery.category_id" placeholder="品类" clearable class="filter-item" style="width: 130px">
        <el-option v-for="item in categoryList" :key="item.id" :label="item.category_name" :value="item.category_id" />
      </el-select>
      <el-select v-model="listQuery.classify_id" placeholder="栏目" clearable class="filter-item" style="width: 130px">
        <el-option v-for="item in classifyList" :key="item.id" :label="item.classify_name" :value="item.classify_id" />
      </el-select>
      <el-select v-model="listQuery.sort" style="width: 140px" class="filter-item" @change="handleFilter">
        <el-option v-for="item in sortOptions" :key="item.key" :label="item.label" :value="item.key" />
      </el-select>
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">
        查询
      </el-button>
      <el-button class="filter-item" style="margin-left: 10px;" type="primary" icon="el-icon-edit" @click="handleCreate">
        新增
      </el-button>
    </div>
    <el-table v-loading="listLoading" :data="list" border fit highlight-current-row style="width: 100%">
      <!-- <el-table-column align="center" label="ID">
        <template slot-scope="scope">
          <span>{{ scope.row.id }}</span>
        </template>
      </el-table-column> -->

      <el-table-column label="封面" align="center">
        <template slot-scope="scope">
          <div>
            <el-image :src="scope.row.image_uri" :preview-src-list="[scope.row.image_uri]" />
          </div>
        </template>
      </el-table-column>

      <el-table-column w label="标题">
        <template slot-scope="{row}">
          <!-- <router-link :to="'/example/edit/'+row.id" class="link-type"> -->
          <span>{{ row.title }}</span>
        </template>
      </el-table-column>

      <el-table-column align="center" label="简介">
        <template slot-scope="scope">
          <router-link :to="'/article/view/'+scope.row.article_id" class="link-type">
            <span>{{ scope.row.content_short }}</span>
          </router-link>
        </template>
      </el-table-column>

      <el-table-column label="星级">
        <template slot-scope="scope">
          <svg-icon v-for="n in +scope.row.importance" :key="n" icon-class="star" class="meta-item__icon" />
        </template>
      </el-table-column>

      <el-table-column label="标签">
        <template slot-scope="scope">
          <el-tag v-for="tag in scope.row.tags" :key="tag.id" type="success">
            <svg-icon :icon-class="tag.svg_name" class="meta-item__icon" />
            <span>{{ tag.tag_name }}</span>
          </el-tag>
        </template>
      </el-table-column>

      <el-table-column label="品类">
        <template slot-scope="scope">
          <span>{{ scope.row.category_name }}</span>
        </template>
      </el-table-column>

      <el-table-column align="center" label="作者">
        <template slot-scope="scope">
          <span>{{ scope.row.author_name }}</span>
        </template>
      </el-table-column>

      <el-table-column class-name="status-col" label="状态">
        <template slot-scope="{row}">
          <el-tag :type="row.status | statusFilter">
            {{ row.status | getStatusLabelFilter }}
          </el-tag>
        </template>
      </el-table-column>

      <el-table-column align="center" label="更新日期">
        <template slot-scope="scope">
          <span>{{ scope.row.updated_at | parseTime('{y}-{m}-{d} {h}:{i}') }}</span>
        </template>
      </el-table-column>

      <el-table-column align="center" label="操作">
        <template slot-scope="{ row }">
          <router-link :to="'/article/edit/'+row.article_id">
            <el-button type="primary" size="small" icon="el-icon-edit">
              编辑
            </el-button>
          </router-link>
          <el-button v-if="row.status!='published'" size="mini" type="success" @click="handleModifyStatus(row,'published')">
            发布
          </el-button>
          <el-button v-if="row.status!='draft'" size="mini" @click="handleModifyStatus(row,'draft')">
            草稿
          </el-button>
          <el-button size="mini" type="danger" @click="handleModifyStatus(row,'deleted')">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />
  </div>
</template>

<script>
import { fetchList, updateArticle, deleteArticle } from '@/api/article'
import waves from '@/directive/waves' // waves directive
import Pagination from '@/components/Pagination' // Secondary package based on el-pagination
import { mapGetters, mapActions } from 'vuex'

export default {
  name: 'ArticleList',
  components: { Pagination },
  directives: { waves },
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'info',
        deleted: 'danger'
      }
      return statusMap[status]
    },
    getStatusLabelFilter(status) {
      const statusMap = {
        published: '发布',
        draft: '草稿'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 20,
        status: undefined,
        name: undefined,
        category_id: undefined,
        classify_id: undefined,
        sort: '+updated_at'
      },
      sortOptions: [{ label: '更新日期+正序', key: '+updated_at' }, { label: '更新日期+倒序', key: '-updated_at' }],
      statusOptions: [
        {
          label: '发布',
          value: 'published'
        },
        {
          label: '草稿',
          value: 'draft'
        }
      ]
    }
  },
  computed: {
    ...mapGetters(['classifyList', 'categoryList'])
  },
  mounted() {
    // console.log("mounted this.listQuery :",this.listQuery);
    this.getList()
    this.getClassifyList()
    this.getCategoryList()
  },
  methods: {
    ...mapActions(['getClassifyList', 'getCategoryList']),
    getList() {
      this.listLoading = true
      fetchList(this.listQuery).then(response => {
        this.list = response.data.items
        this.total = response.data.total
        this.listLoading = false
      })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    handleCreate() {
      this.$router.push('/article/create')
    },
    handleModifyStatus(row, status) {
      var statusData = {
        id: row.id,
        article_id: row.article_id,
        status: status
      }
      if (status === 'deleted') {
        deleteArticle(statusData).then((response) => {
          this.$message({
            message: '删除成功',
            type: 'success'
          })
          this.getList()
        })
        return
      }
      updateArticle(statusData).then((response) => {
        this.getList()
        this.$message({
          message: '操作Success',
          type: 'success'
        })
        // row.status = status
      })
    }
  }
}
</script>

<style scoped>
.edit-input {
  padding-right: 100px;
}
.cancel-btn {
  position: absolute;
  right: 15px;
  top: 10px;
}
</style>
